//
//  CertifiPhotoView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/24.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class CertifiPhotoView: UIView, UITableViewDataSource, UITableViewDelegate{
    var dataSource: CertifiPhotoViewController!
    var selectSection:Int = 0
    lazy var tableView:UITableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)
    
    lazy var selectBtn:UIButton = {
        let btn =  CommonBtn.creatSelectBtnWithTitle(title: kText("确认"), y: 25, view: self, sel:  #selector(selectBtnAction))
        return btn
    }()
    
    lazy var headView:UIView = {
       let view = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 40))
       return view
    }()
    
    lazy var titleLabel:UILabel = {
       let label = UILabel()
       label.font = UIFont.systemFont(ofSize: 12)
       label.textColor = KCommonTextColor
       return label
    }()

    init(dataSource: CertifiPhotoViewController!) {
        super.init(frame: CGRect.zero)
        
        self.dataSource = dataSource
        self.themeBackground = ThemeManager.ThemeColorKeys.background
        self.setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() -> Void {
        
        self.addSubview(tableView)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.clear
        tableView.tableHeaderView = headView
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalToSuperview()
        }
        
        headView.addSubview(titleLabel)
        let name = String(format: "*%@",(dataSource.viewModel?.name.count)! > 1 ?
            ((dataSource.viewModel?.name as NSString?)?.substring(from: (dataSource.viewModel?.name.count)! - 1))! : "")
        titleLabel.text = String(format: "%@ %@ 的%@",kText("请上传"), name,(dataSource.viewModel?.isChinese)!
            ? kText("身份证正反面照片"): kText("护照照片"))
        titleLabel.changeLabelTextStyle(title: name, color: kThemeColor, font: UIFont.systemFont(ofSize: 12))
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        let footView = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 100))
        tableView.tableFooterView = footView
        footView.addSubview(selectBtn)
//        selectBtn.isEnabled = false
//        CommonBtn.createImageWithColor(color: kDisableColor, btn: selectBtn, state: UIControlState.normal)
    }
    
    //MARK: tableViewDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identy = "identy"
        var cell = tableView.dequeueReusableCell(withIdentifier: identy) as? SelectPhotoCell
        if cell == nil {
            cell = SelectPhotoCell(style: .default, reuseIdentifier: identy)
            cell?.selectionStyle = .none
            cell?.tag = indexPath.section + KCellTage
        }
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150
    }
//    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        let number = section == 0 ? 0 : 30
//        return CGFloat(number)
//    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 55
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view:UIView = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 55))
        let label = UILabel()
        label.text = section == 0 ? kText("上传身份证正面照片") : kText("上传身份证反面照片")
        if  !(dataSource.viewModel?.isChinese)!  {
            label.text = kText("上传护照照片")
        }
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = KCommonTextColor
        view.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.centerY.centerX.equalToSuperview()
        }
        return view
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectSection = indexPath.section
        self.selectPhoto(sender: UIButton())
    }
    
    @objc func selectPhoto(sender:UIButton) ->Void {
        let cell:SelectPhotoCell = self.viewWithTag(KCellTage + selectSection) as! SelectPhotoCell
        SelectPhotoManager.manager.selectPhoto(dataSource: dataSource, maximum: 1, haveNb: 0) { (arr) in
            if arr.count == 0 {
                return
            }
            let image:UIImage = UIImage(imageSimple: arr.last!, scaledTo: cell.photoImageView.frame.size)
            cell.photoImageView.image = image
        }
    }
    
    @objc func selectBtnAction() -> Void {
        
        let fortCell:SelectPhotoCell = self.viewWithTag(KCellTage) as! SelectPhotoCell
        if fortCell.photoImageView.image == nil {
            CYMBShow.showMessage(kText("请上传身份证正面图片"))
            return
        }
        let backCell:SelectPhotoCell = self.viewWithTag(KCellTage + 1) as! SelectPhotoCell
        if backCell.photoImageView.image == nil {
            CYMBShow.showMessage(kText("请上传身份证反面图片"))
            return
        }
        dataSource.viewModel?.backImage = backCell.photoImageView.image!
        dataSource.viewModel?.fortImage = fortCell.photoImageView.image!
        dataSource.viewModel?.sendPhoto()
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
